package com.ctshk.rpc.order.season.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.OrderCancelStatus;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.season.dto.SeasonOrderRefundDTO;
import com.ctshk.rpc.order.season.entity.SeasonOrder;
import com.ctshk.rpc.order.season.entity.SeasonOrderRefund;
import com.ctshk.rpc.order.season.mapper.SeasonOrderMapper;
import com.ctshk.rpc.order.season.mapper.SeasonOrderRefundMapper;
import com.ctshk.rpc.order.season.service.ISeasonOrderRefundService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 套票退款记录 服务实现类
 * </p>
 *
 * @author 杨林
 * @since 2021-03-24
 */
@DubboService
public class SeasonOrderRefundServiceImpl extends ServiceImpl<SeasonOrderRefundMapper, SeasonOrderRefund> implements ISeasonOrderRefundService {


    @Autowired
    private  SeasonOrderRefundMapper  orderRefundMapper;


    @Autowired
    private SeasonOrderMapper  seasonOrderMapper;
    
    /* @MethodName: 订单退款记录
     * @Description: TODO
     * @Param: 
     * @Return: 
     * @Author: 杨林
     * @Date:2021/3/30 14:55
    **/
    @Override
    public List<SeasonOrderRefundDTO> getOrderRefundByOrderId(Long id) {
        List<SeasonOrderRefundDTO> resultList=
                EntityUtil.copyList( orderRefundMapper.selectList(
                new QueryWrapper<SeasonOrderRefund>().lambda()
                        .eq(SeasonOrderRefund::getOrderId,id)
                        .eq(SeasonOrderRefund::getIsDeleted, IsDeletedCode.NO.getCode())
        ),SeasonOrderRefundDTO.class);

        for(SeasonOrderRefundDTO refundDTO:resultList){
            String statusName="";
            int status=refundDTO.getStatus().intValue();
            switch (status){
                case 1:
                    statusName="审批中";
                    break;

                case 2:
                    statusName="审批成功";
                    break;
                case 3:
                    statusName="审批失败";
                    break;
                case 4:
                    statusName="退款成功";
                    break;
            }
            refundDTO.setStatusName(statusName);
        }
        return resultList;
    }

    /* @MethodName: 操作现金退款
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/30 15:20
     **/
    @Override
    public Result refundCash(Long id, TokenUser user) {
        //退款详情
        SeasonOrderRefund  refund=orderRefundMapper.selectById(id);
        if(Objects.isNull(refund)){
             return Result.failed(SystemError.SEASON_TICKET_20024);
        }
        //退款方式
        String refundWay=refund.getRefundWay();
        if(!"现金".equals(refundWay)){
            return Result.failed(SystemError.SEASON_TICKET_20025);
        }
        refund.setStatus(OrderCancelStatus.ALREADY_REFUND.getCode());
        refund.setGmtModified(LocalDateTime.now());
        refund.setModifiedId(user.getId());

        SeasonOrder  order=seasonOrderMapper.selectById(refund.getOrderId());
        if(Objects.isNull(order)){
            return Result.failed(SystemError.SEASON_TICKET_20021);
        }
        BigDecimal refundedAmount=order.getRefundedAmount();//已退金额
        refundedAmount.add(refund.getRefundAmount());//现金就是港币
        order.setRefundedAmount(refundedAmount);
        seasonOrderMapper.updateById(order);

        return Result.success();
    }
}
